import Vue from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios';
import uploader from '../src'
import Bus from './config/bus'
import {
    debounce
} from './plugins/common'
import './plugins/echarts'

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(uploader)

Vue.use(ElementUI);

Vue.prototype.$http = axios;

Vue.prototype.$Bus = Bus;

Vue.prototype.$systemMenu = window.localStorage.systemMenu ? JSON.parse(window.localStorage.systemMenu) : {}

Vue.prototype.$updata = ''

Vue.prototype.$map = "http://121.89.201.182:6080/";

Vue.prototype.$jsApi = "http://121.89.201.182:6080/";

Vue.prototype.$proxy = "http://121.89.201.182:6080/";

axios.defaults.headers.token = window.localStorage.token ? window.localStorage.token : ''

axios.defaults.timeout = 30000;

Vue.prototype.$mes = debounce(function(item) {
    Vue.prototype.$message(item)
}, 500)


Vue.prototype.$post = function(url, body) {

    return axios.post(url, body).then(res => {
        if (res.data.code == 400) {
            if (this.$route.path != '/login') { //解决 在登录页刷新后弹出token过期请从新登录bug
                this.$mes({
                    message: '登录超时请重新登录',
                    type: "error"
                })
            }
            setTimeout(() => {
                this.$router.push('/login')
            }, 1000);
        }
        return res.data
    }).catch(error => {
        console.log(typeof error)
        if (/30000ms/.test(error)) {
            this.$message({
                message: '请求超时',
                code: 'error'
            })
        }
        return error
    })


}
Vue.prototype.$get = function(url, params = {}) {
    return axios.get(url, {
        params
    }).then(res => {
        if (res.data.code == 400) {
            if (this.$route.path != '/login') {
                this.$mes({
                    message: '登录超时请重新登录',
                    type: "error"
                })
            }
            setTimeout(() => {
                this.$router.push('/login')
            }, 1000);
        }
        return res.data
    })
}



const excludeRoute = ['/login'];
let systemMenu = window.localStorage.systemMenu ? JSON.parse(window.localStorage.systemMenu) : {}

Vue.prototype.$reloadSystemMenu = function() {
        axios.defaults.headers.token = window.localStorage.token ? window.localStorage.token : ''
        systemMenu = window.localStorage.systemMenu ? JSON.parse(window.localStorage.systemMenu) : {}
        Vue.prototype.$systemMenu = window.localStorage.systemMenu ? JSON.parse(window.localStorage.systemMenu) : {}
    }
    //路由守卫
    //1 登陆验证-Token-userId

router.beforeEach((to, from, next) => {

    if (excludeRoute.findIndex(v => v === to.path) < 0) {
        //以下为验证条件
        var token = window.localStorage.token;
        var userid = window.localStorage.userid;
        //无用户ID和TOKEN 跳转到登录
        if (!token || !userid) {
            window.localStorage.removeItem('token')
            window.localStorage.removeItem('userid')
            next({
                path: "/login"
            })
            return
        }
    }
    if (to.path != "/home") {
        if (excludeRoute.findIndex(v => v === to.path) < 0 && !systemMenu[to.meta.title]) {
            //判断路由权限
            next({
                path: "/"
            })
            return
        }
    }
    next();
});


Vue.config.productionTip = false

new Vue({
    router,
    render: h => h(App)
}).$mount('#app')